using System;
using System.ComponentModel;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;

namespace SDRSharp.RTLTCP
{
	public class RTLTcpSettings : Form
	{
		private readonly RtlTcpIO _owner;

		private IContainer components;

		private TextBox hostBox;

		private Label label1;

		private Label label2;

		private CheckBox rtlAgcCheckBox;

		private Label label6;

		private NumericUpDown frequencyCorrectionNumericUpDown;

		private CheckBox tunerAgcCheckBox;

		private Label label7;

		private TrackBar tunerGainTrackBar;

		private Label label8;

		private ComboBox samplerateComboBox;

		private Timer refreshTimer;

		private NumericUpDown portNumberUpDown;

		private Label tunerLabel;

		public string Hostname
		{
			get
			{
				return this.hostBox.Text;
			}
			set
			{
				this.hostBox.Text = value;
			}
		}

		public int Port
		{
			get
			{
				return (int)this.portNumberUpDown.Value;
			}
			set
			{
				this.portNumberUpDown.Value = value;
			}
		}

		public RTLTcpSettings(RtlTcpIO owner)
		{
			this._owner = owner;
			this.InitializeComponent();
			this.samplerateComboBox.SelectedIndex = 3;
			this.rtlAgcCheckBox.Checked = false;
			this.tunerAgcCheckBox.Checked = false;
			this.UpdateGuiState();
		}

		private void refreshTimer_Tick(object sender, EventArgs e)
		{
			this.UpdateGuiState();
		}

		private void UpdateGuiState()
		{
			this.tunerGainTrackBar.Enabled = (this._owner.IsStreaming && !this.tunerAgcCheckBox.Checked);
			this.tunerAgcCheckBox.Enabled = this._owner.IsStreaming;
			this.samplerateComboBox.Enabled = !this._owner.IsStreaming;
			this.hostBox.Enabled = !this._owner.IsStreaming;
			this.portNumberUpDown.Enabled = !this._owner.IsStreaming;
			this.Text = this._owner.TunerName;

			this.tunerLabel.Text = (this._owner.IsStreaming ? this._owner.TunerType.ToString() : string.Empty);
			if ((long)this.tunerGainTrackBar.Value > (long)((ulong)this._owner.TunerGainCount))
			{
				this.tunerGainTrackBar.Value = 0;
			}
			if ((long)this.tunerGainTrackBar.Maximum != (long)((ulong)this._owner.TunerGainCount))
			{
				this.tunerGainTrackBar.Maximum = (int)this._owner.TunerGainCount;
				//this.tunerGainTrackBar.Maximum = 70;
				//this.tunerLabel.Text = "70dB";
			}
		}

		private void RTLTcpSettings_VisibleChanged(object sender, EventArgs e)
		{
			this.refreshTimer.Enabled = base.Visible;
			this.UpdateGuiState();
		}

		private void RTLTcpSettings_FormClosing(object sender, FormClosingEventArgs e)
		{
			e.Cancel = true;
			base.Hide();
		}

		private void samplerateComboBox_SelectedIndexChanged(object sender, EventArgs e)
		{
			double num = double.Parse(this.samplerateComboBox.Items[this.samplerateComboBox.SelectedIndex].ToString().Split(new char[]
			{
				' '
			})[0], CultureInfo.InvariantCulture);
			this._owner.Samplerate = (uint)(num * 1000000.0);
		}

		private void rtlAgcCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			this._owner.UseRtlAGC = this.rtlAgcCheckBox.Checked;
		}

		private void tunerAgcCheckBox_CheckedChanged(object sender, EventArgs e)
		{
			this.tunerGainTrackBar.Enabled = (this.tunerAgcCheckBox.Enabled && !this.tunerAgcCheckBox.Checked);
			this._owner.UseTunerAGC = this.tunerAgcCheckBox.Checked;
		}

		private void tunerGainTrackBar_Scroll(object sender, EventArgs e)
		{
			this._owner.TunerGainIndex = (uint)this.tunerGainTrackBar.Value;
		}

		private void frequencyCorrectionNumericUpDown_ValueChanged(object sender, EventArgs e)
		{
			this._owner.FrequencyCorrection = (int)this.frequencyCorrectionNumericUpDown.Value;
		}

		protected override void Dispose(bool disposing)
		{
			if (disposing && this.components != null)
			{
				this.components.Dispose();
			}
			base.Dispose(disposing);
		}

		private void InitializeComponent()
		{
			this.components = new Container();
			this.hostBox = new TextBox();
			this.label1 = new Label();
			this.label2 = new Label();
			this.rtlAgcCheckBox = new CheckBox();
			this.label6 = new Label();
			this.frequencyCorrectionNumericUpDown = new NumericUpDown();
			this.tunerAgcCheckBox = new CheckBox();
			this.label7 = new Label();
			this.tunerGainTrackBar = new TrackBar();
			this.label8 = new Label();
			this.samplerateComboBox = new ComboBox();
			this.refreshTimer = new Timer(this.components);
			this.portNumberUpDown = new NumericUpDown();
			this.tunerLabel = new Label();
			((ISupportInitialize)this.frequencyCorrectionNumericUpDown).BeginInit();
			((ISupportInitialize)this.tunerGainTrackBar).BeginInit();
			((ISupportInitialize)this.portNumberUpDown).BeginInit();
			base.SuspendLayout();
			this.hostBox.Location = new Point(119, 12);
			this.hostBox.Name = "hostBox";
			this.hostBox.Size = new Size(138, 20);
			this.hostBox.TabIndex = 0;
			this.hostBox.Text = "127.0.0.1";
			this.label1.AutoSize = true;
			this.label1.Location = new Point(7, 12);
			this.label1.Name = "label1";
			this.label1.Size = new Size(29, 13);
			this.label1.TabIndex = 4;
			this.label1.Text = "Host";
			this.label2.AutoSize = true;
			this.label2.Location = new Point(7, 38);
			this.label2.Name = "label2";
			this.label2.Size = new Size(26, 13);
			this.label2.TabIndex = 5;
			this.label2.Text = "Port";
			this.rtlAgcCheckBox.AutoSize = true;
			this.rtlAgcCheckBox.Location = new Point(10, 111);
			this.rtlAgcCheckBox.Name = "rtlAgcCheckBox";
			this.rtlAgcCheckBox.Size = new Size(72, 17);
			this.rtlAgcCheckBox.TabIndex = 4;
			this.rtlAgcCheckBox.Text = "RTL AGC";
			this.rtlAgcCheckBox.UseVisualStyleBackColor = true;
			this.rtlAgcCheckBox.CheckedChanged += new EventHandler(this.rtlAgcCheckBox_CheckedChanged);
			this.label6.AutoSize = true;
			this.label6.Location = new Point(10, 218);
			this.label6.Name = "label6";
			this.label6.Size = new Size(136, 13);
			this.label6.TabIndex = 35;
			this.label6.Text = "Frequency correction (ppm)";
			this.frequencyCorrectionNumericUpDown.Location = new Point(167, 216);
			NumericUpDown arg_304_0 = this.frequencyCorrectionNumericUpDown;
			int[] expr_2F7 = new int[4];
			expr_2F7[0] = 1000;
			arg_304_0.Maximum = new decimal(expr_2F7);
			this.frequencyCorrectionNumericUpDown.Minimum = new decimal(new int[]
			{
				1000,
				0,
				0,
				-2147483648
			});
			this.frequencyCorrectionNumericUpDown.Name = "frequencyCorrectionNumericUpDown";
			this.frequencyCorrectionNumericUpDown.Size = new Size(90, 20);
			this.frequencyCorrectionNumericUpDown.TabIndex = 7;
			this.frequencyCorrectionNumericUpDown.TextAlign = HorizontalAlignment.Right;
			this.frequencyCorrectionNumericUpDown.ValueChanged += new EventHandler(this.frequencyCorrectionNumericUpDown_ValueChanged);
			this.tunerAgcCheckBox.AutoSize = true;
			this.tunerAgcCheckBox.Checked = true;
			this.tunerAgcCheckBox.CheckState = CheckState.Checked;
			this.tunerAgcCheckBox.Location = new Point(10, 134);
			this.tunerAgcCheckBox.Name = "tunerAgcCheckBox";
			this.tunerAgcCheckBox.Size = new Size(79, 17);
			this.tunerAgcCheckBox.TabIndex = 5;
			this.tunerAgcCheckBox.Text = "Tuner AGC";
			this.tunerAgcCheckBox.UseVisualStyleBackColor = true;
			this.tunerAgcCheckBox.CheckedChanged += new EventHandler(this.tunerAgcCheckBox_CheckedChanged);
			this.label7.AutoSize = true;
			this.label7.Location = new Point(10, 154);
			this.label7.Name = "label7";
			this.label7.Size = new Size(46, 13);
			this.label7.TabIndex = 33;
			this.label7.Text = "RF Gain";
			this.tunerGainTrackBar.Enabled = false;
			this.tunerGainTrackBar.Location = new Point(1, 170);
			this.tunerGainTrackBar.Maximum = 10000;
			this.tunerGainTrackBar.Name = "tunerGainTrackBar";
			this.tunerGainTrackBar.Size = new Size(267, 45);
			this.tunerGainTrackBar.TabIndex = 6;
			this.tunerGainTrackBar.Scroll += new EventHandler(this.tunerGainTrackBar_Scroll);
			this.label8.AutoSize = true;
			this.label8.Location = new Point(7, 66);
			this.label8.Name = "label8";
			this.label8.Size = new Size(68, 13);
			this.label8.TabIndex = 37;
			this.label8.Text = "Sample Rate";
			this.samplerateComboBox.DropDownStyle = ComboBoxStyle.DropDownList;
			this.samplerateComboBox.FormattingEnabled = true;
			this.samplerateComboBox.Items.AddRange(new object[]
			{
				"12.0 MSPS",
				"3.2 MSPS",
				"2.8 MSPS",
				"2.4 MSPS",
				"2.048 MSPS",
				"1.92 MSPS",
				"1.8 MSPS",
				"1.4 MSPS",
				"1.024 MSPS",
				"0.900001 MSPS",
				"0.25 MSPS"
			});
			this.samplerateComboBox.Location = new Point(10, 84);
			this.samplerateComboBox.Name = "samplerateComboBox";
			this.samplerateComboBox.Size = new Size(247, 21);
			this.samplerateComboBox.TabIndex = 3;
			this.samplerateComboBox.SelectedIndexChanged += new EventHandler(this.samplerateComboBox_SelectedIndexChanged);
			this.refreshTimer.Interval = 1000;
			this.refreshTimer.Tick += new EventHandler(this.refreshTimer_Tick);
			this.portNumberUpDown.Location = new Point(119, 38);
			NumericUpDown arg_692_0 = this.portNumberUpDown;
			int[] expr_685 = new int[4];
			expr_685[0] = 65535;
			arg_692_0.Maximum = new decimal(expr_685);
			this.portNumberUpDown.Name = "portNumberUpDown";
			this.portNumberUpDown.Size = new Size(138, 20);
			this.portNumberUpDown.TabIndex = 2;
			NumericUpDown arg_6E3_0 = this.portNumberUpDown;
			int[] expr_6D6 = new int[4];
			expr_6D6[0] = 1234;
			arg_6E3_0.Value = new decimal(expr_6D6);
			this.tunerLabel.Location = new Point(186, 154);
			this.tunerLabel.Name = "tunerLabel";
			this.tunerLabel.Size = new Size(68, 13);
			this.tunerLabel.TabIndex = 38;
			this.tunerLabel.Text = "Tuner";
			this.tunerLabel.TextAlign = ContentAlignment.MiddleRight;
			base.AutoScaleDimensions = new SizeF(6f, 13f);
			base.AutoScaleMode = AutoScaleMode.Font;
			base.ClientSize = new Size(266, 257);
			base.Controls.Add(this.tunerLabel);
			base.Controls.Add(this.portNumberUpDown);
			base.Controls.Add(this.label8);
			base.Controls.Add(this.samplerateComboBox);
			base.Controls.Add(this.rtlAgcCheckBox);
			base.Controls.Add(this.label6);
			base.Controls.Add(this.frequencyCorrectionNumericUpDown);
			base.Controls.Add(this.tunerAgcCheckBox);
			base.Controls.Add(this.label7);
			base.Controls.Add(this.tunerGainTrackBar);
			base.Controls.Add(this.label2);
			base.Controls.Add(this.label1);
			base.Controls.Add(this.hostBox);
			base.FormBorderStyle = FormBorderStyle.FixedDialog;
			base.MaximizeBox = false;
			base.MinimizeBox = false;
			base.Name = "RTLTcpSettings";
			base.ShowInTaskbar = false;
			this.Text = "RTL-TCP Settings";
			base.TopMost = true;
			base.FormClosing += new FormClosingEventHandler(this.RTLTcpSettings_FormClosing);
			base.VisibleChanged += new EventHandler(this.RTLTcpSettings_VisibleChanged);
			((ISupportInitialize)this.frequencyCorrectionNumericUpDown).EndInit();
			((ISupportInitialize)this.tunerGainTrackBar).EndInit();
			((ISupportInitialize)this.portNumberUpDown).EndInit();
			base.ResumeLayout(false);
			base.PerformLayout();
		}
	}
}
